package com.wzk.travelspringboot.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzk.travelspringboot.dto.Result;
import com.wzk.travelspringboot.entity.BlogComment;
import com.wzk.travelspringboot.service.BlogCommentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @author wangzikang
 */
@Slf4j
@RestController
@RequestMapping("/admin/blogComment")
@RequiredArgsConstructor
@Validated
public class AdminBlogCommentController {

    private final BlogCommentService blogCommentService;

    /**
     * 分页查询所有博客评论
     */
    @GetMapping("/list")
    public Result listBlogComments(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<BlogComment> page = new Page<>(pageNum, pageSize);
        Page<BlogComment> commentPage = blogCommentService.page(page);
        return Result.success(commentPage);
    }

    /**
     * 根据博客ID查询评论
     */
    @GetMapping("/blog/{blogId}")
    public Result getCommentsByBlogId(
            @PathVariable("blogId") @NotNull(message = "博客ID不能为空") Long blogId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<BlogComment> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BlogComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlogComment::getBlogId, blogId);
        queryWrapper.eq(BlogComment::getIsDelete, 0);
        Page<BlogComment> commentPage = blogCommentService.page(page, queryWrapper);
        return Result.success(commentPage);
    }

    /**
     * 根据用户ID查询评论
     */
    @GetMapping("/user/{userId}")
    public Result getCommentsByUserId(
            @PathVariable("userId") @NotNull(message = "用户ID不能为空") Long userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<BlogComment> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BlogComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlogComment::getUserId, userId);
        queryWrapper.eq(BlogComment::getIsDelete, 0);
        Page<BlogComment> commentPage = blogCommentService.page(page, queryWrapper);
        return Result.success(commentPage);
    }

    /**
     * 根据ID删除/隐藏评论
     */
    @DeleteMapping("/remove/{id}")
    public Result hideBlogComment(@PathVariable("id") @NotNull(message = "评论ID不能为空") Long id) {
        BlogComment comment = new BlogComment();
        comment.setId(id);
        comment.setIsDelete(1);
        boolean success = blogCommentService.updateById(comment);
        return success ? Result.success("隐藏评论成功") : Result.failed("隐藏评论失败");
    }

    /**
     * 根据ID恢复评论
     */
    @PostMapping("/recover/{id}")
    public Result recoverBlogComment(@PathVariable("id") @NotNull(message = "评论ID不能为空") Long id) {
        BlogComment comment = new BlogComment();
        comment.setId(id);
        comment.setIsDelete(0);
        boolean success = blogCommentService.updateById(comment);
        return success ? Result.success("恢复评论成功") : Result.failed("恢复评论失败");
    }

    /**
     * 根据内容模糊查询评论
     */
    @GetMapping("/search")
    public Result searchCommentByContent(
            @RequestParam String content,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<BlogComment> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BlogComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(BlogComment::getContent, content);
        queryWrapper.eq(BlogComment::getIsDelete, 0);
        Page<BlogComment> commentPage = blogCommentService.page(page, queryWrapper);
        return Result.success(commentPage);
    }
} 